package com.shuoyue.adven.main.bussiness;

import com.shuoyue.adven.entity.Adv;
import com.shuoyue.adven.entity.ImpotentResult;
import com.shuoyue.adven.entity.Property;
import com.shuoyue.adven.network.RetrofitClient;
import com.sm.appbase.base.basemvp.BaseModel;
import com.sm.appbase.base.basemvp.BasePresenter;
import com.sm.appbase.base.basemvp.BaseView;
import com.sm.appbase.net.base.ApiSubscriber;
import com.sm.appbase.net.base.BaseResult;
import com.sm.appbase.net.base.Optional;

import java.util.HashMap;
import java.util.List;

import io.reactivex.Observable;

public interface MainContract {
    public interface View extends BaseView {
        void queryBusinessAdvSuc(List<Adv> mData);

        void queryPropertyAdvSuc(List<Adv> mdata);

        void queryImpotentAdvSuc(ImpotentResult mdata);

        void getPropertyInfoSuc(Property property);
    }

    public class Model extends BaseModel {
        Observable<BaseResult<List<Adv>>> queryPropertyAdv() {
            return RetrofitClient.getInstance().getApi().queryPropertyAdv();
        }

        Observable<BaseResult<List<Adv>>> queryBusinessAdv() {
            return RetrofitClient.getInstance().getApi().queryBusinessAdv();
        }

        Observable<BaseResult<ImpotentResult>> queryImpotentAdv() {
            return RetrofitClient.getInstance().getApi().queryImpotentAdv();
        }

        Observable<BaseResult<Property>> getPropertyInfo() {
            return RetrofitClient.getInstance().getApi().getPropertyInfo();
        }

        Observable<BaseResult<String[]>> uploadShow(int id) {
            HashMap<String, Integer> hashMap = new HashMap();
            hashMap.put("adId", id);
            return RetrofitClient.getInstance().getApi().uploadBusinessPlayState(hashMap);
        }
    }

    class Presenter extends BasePresenter<View> {
        Model model = new Model();

        public void queryPropertyAdv() {
            apply(model.queryPropertyAdv())
                    .subscribe(new ApiSubscriber<Optional<List<Adv>>>(mView, this, false) {
                        @Override
                        public void onNext(Optional<List<Adv>> listOptional) {
                            super.onNext(listOptional);
                            mView.queryPropertyAdvSuc(listOptional.getIncludeNull());
                        }
                    });
        }

        public void queryBusinessAdv() {
            apply(model.queryBusinessAdv())
                    .subscribe(new ApiSubscriber<Optional<List<Adv>>>(mView, this, false) {
                        @Override
                        public void onNext(Optional<List<Adv>> listOptional) {
                            super.onNext(listOptional);
                            mView.queryBusinessAdvSuc(listOptional.getIncludeNull());
                        }
                    });
        }

        public void queryImpotentAdv() {
            apply(model.queryImpotentAdv())
                    .subscribe(new ApiSubscriber<Optional<ImpotentResult>>(mView, this, false) {
                        @Override
                        public void onNext(Optional<ImpotentResult> listOptional) {
                            super.onNext(listOptional);
                            mView.queryImpotentAdvSuc(listOptional.getIncludeNull());
                        }
                    });
        }

        public void getPropertyInfo() {
            apply(model.getPropertyInfo())
                    .subscribe(new ApiSubscriber<Optional<Property>>(mView, this, false) {
                        @Override
                        public void onNext(Optional<Property> listOptional) {
                            super.onNext(listOptional);
                            mView.getPropertyInfoSuc(listOptional.getIncludeNull());
                        }
                    });
        }

        public void uploadShow(int id) {
            apply(model.uploadShow(id))
                    .subscribe(new ApiSubscriber<Optional<String[]>>(mView, this, false) {
                        @Override
                        public void onNext(Optional<String[]> stringOptional) {
                            super.onNext(stringOptional);
                        }
                    });
        }
    }
}
